<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="工单号" prop="workOrderNo">
        <el-input v-model="queryParams.workOrderNo" placeholder="请输入工单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="计划编号" prop="planNo">
        <el-input v-model="queryParams.planNo" placeholder="请输入计划编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="车间" prop="deptId">
        <treeselect style="width: 200px" v-model="queryParams.deptId" :options="deptList" :normalizer="normalizer"
                    placeholder="请选择车间" clearable/>
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品编码" prop="productCode">
        <el-input v-model="queryParams.productCode" placeholder="请输入产品编码" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="物料编码" prop="materialNo">
        <el-input v-model="queryParams.materialNo" placeholder="请输入物料编码" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input v-model="queryParams.materialName" placeholder="请输入物料名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="工序编号" prop="procedureCode">
        <el-input v-model="queryParams.procedureCode" placeholder="请输入工序编号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="工序名称" prop="procedureName">
        <el-input v-model="queryParams.procedureName" placeholder="请输入工序名称" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="设备编号" prop="deviceCode">
        <el-input v-model="queryParams.deviceCode" placeholder="请输入设备编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.WORKORDER_STATUS)" :key="dict.value" :label="dict.label"
                     :value="parseInt(dict.value)"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openForm(undefined)"-->
      <!--                   v-hasPermi="['production:work-order:create']">新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"-->
      <!--                   :loading="exportLoading"-->
      <!--                   v-hasPermi="['production:work-order:export']">导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <!--        <el-button-->
        <!--          type="danger"-->
        <!--          plain-->
        <!--          icon="el-icon-delete"-->
        <!--          size="mini"-->
        <!--          :disabled="isEmpty(checkedIds)"-->
        <!--          @click="handleDeleteBatch"-->
        <!--          v-hasPermi="['production:work-order:delete']"-->
        <!--        >-->
        <!--          批量删除-->
        <!--        </el-button>-->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange"
    >
      <el-table-column type="selection" width="55"/>
      <el-table-column label="工单号" align="center" prop="workOrderNo" width="200"/>
      <el-table-column label="计划编号" align="center" prop="planNo" width="200"/>
      <el-table-column label="车间名称" align="center" prop="deptName" width="200"/>
      <el-table-column label="产品名称" align="center" prop="productName" width="200"/>
      <el-table-column label="产品编码" align="center" prop="productCode" width="200"/>
      <el-table-column label="物料编码" align="center" prop="materialNo" width="200"/>
      <el-table-column label="物料名称" align="center" prop="materialName" width="200"/>
      <el-table-column label="工序编号" align="center" prop="procedureCode" width="200"/>
      <el-table-column label="工序名称" align="center" prop="procedureName" width="200"/>
      <el-table-column label="设备编号" align="center" prop="deviceCode" width="200"/>
      <el-table-column label="设备名称" align="center" prop="deviceName" width="200"/>
      <el-table-column label="工单顺序" align="center" prop="sort" width="200"/>
      <el-table-column label="工单数量" align="center" prop="workOrderQuantity" width="200"/>
      <el-table-column label="计划开始时间" align="center" prop="planBeginTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.planBeginTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划结束时间" align="center" prop="planEndTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.planEndTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际开始时间" align="center" prop="actualBeginTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.actualBeginTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际结束时间" align="center" prop="actualEndTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.actualEndTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已完成数量" align="center" prop="completeQuantity" width="200"/>
      <el-table-column label="每日派工数量" align="center" prop="daySendWorkNumber" width="200"/>
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <span>{{ getDictDataLabel(DICT_TYPE.WORKORDER_STATUS, scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="210">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="openForm(scope.row.id)"
                     v-hasPermi="['production:work-order:update']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-document" @click="toDetail(scope.row.id)"
                     v-hasPermi="['production:work-order:detail']">工单详情
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['production:work-order:delete']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    <!-- 对话框(添加 / 修改) -->
    <WorkOrderForm ref="formRef" @success="getList"/>
  </div>
</template>

<script>
import * as WorkOrderApi from '@/api/production/workorder';
import WorkOrderForm from './WorkOrderForm.vue';
import {listDept} from "@/api/system/dept";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Treeselect from "@riophae/vue-treeselect";

export default {
  name: "WorkOrder",
  components: {
    WorkOrderForm,
    Treeselect
  },
  data() {
    return {
      deptList: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工单列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      checkedIds: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        workOrderNo: null,
        procedureWorkOrderId: null,
        procedureWorkOrderNo: null,
        planId: null,
        planNo: null,
        deptId: null,
        deptName: null,
        productId: null,
        productName: null,
        productCode: null,
        materialId: null,
        materialNo: null,
        materialName: null,
        procedureId: null,
        procedureCode: null,
        procedureName: null,
        deviceId: null,
        deviceCode: null,
        deviceName: null,
        planBeginTime: [],
        planEndTime: [],
        actualBeginTime: [],
        actualEndTime: [],
        daySendWorkNumber: null,
        status: null,
        createTime: [],
      },
    };
  },
  created() {
    this.getList();
    this.getDeptList();
  },
  methods: {
    /** 获取部门列表 */
    getDeptList() {
      listDept().then(response => {
        this.deptList = this.handleTree(response.data, "id")[0]?.children || [];
      });
    },

    /** 部门节点格式化 */
    normalizer(node) {
      return {
        id: node.id,
        label: node.name,
        children: node.children,
      };
    },
    toDetail(id) {
      this.$router.push({
        path: '/production/detail',
        query: {id: id}
      });
    },
    /** 查询列表 */
    async getList() {
      try {
        this.loading = true;
        const res = await WorkOrderApi.getWorkOrderPage(this.queryParams);
        this.list = res.data.list;
        this.total = res.data.total;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除工单编号为"' + id + '"的数据项?')
      try {
        await WorkOrderApi.deleteWorkOrder(id);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {
      }
    },
    /** 批量删除工单 */
    async handleDeleteBatch() {
      await this.$modal.confirm('是否确认删除?')
      try {
        await WorkOrderApi.deleteWorkOrderList(this.checkedIds);
        this.checkedIds = [];
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {
      }
    },
    handleRowCheckboxChange(records) {
      this.checkedIds = records.map((item) => item.id);
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm('是否确认导出所有工单数据项?');
      try {
        this.exportLoading = true;
        const data = await WorkOrderApi.exportWorkOrderExcel(this.queryParams);
        this.$download.excel(data, '工单.xls');
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },
  }
};
</script>
